package com.zzyl.serve.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.serve.domain.PageResult;
import com.zzyl.serve.vo.ProjectAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 获取所有护理项目
     *
     * @return
     */
    @Override
    public List<ProjectAll> getAllProject() {
        //1.获取所有的护理项目
        LambdaQueryWrapper<NursingProject> queryWrapper = Wrappers.<NursingProject>lambdaQuery().eq(NursingProject::getStatus, 1);
        List<NursingProject> nursingProjects = nursingProjectMapper.selectList(queryWrapper);
        //2.遍历nursingProjects,将名字和id封装到ProjectAll中
        //2.1创建ProjectAll集合
        List<ProjectAll> projectAlls = new ArrayList<>();
        //2.2遍历
        nursingProjects.forEach(nursingProject -> {
            ProjectAll projectAll = new ProjectAll();
            //2.3将值设置进去
            projectAll.setLabel(nursingProject.getName());
            projectAll.setValue(nursingProject.getId().toString());
            projectAlls.add(projectAll);
        });
        return projectAlls;
    }

    @Override
    public AjaxResult getPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> list = nursingProjectMapper.getPage(status, name);
        AjaxResult aj = new AjaxResult();
        aj.put("code", 200);

        aj.put("rows", list.getResult());
        aj.put("total", list.getTotal());
        aj.put("msg", "成功");

        return aj;
    }
}
